package com.boc.langxiang.presenter.me;

import com.boc.langxiang.contract.me.OrdersApplyRefundContract;
import com.boc.langxiang.entity.request.ReturnOrdersPageRequest;
import com.boc.langxiang.entity.request.ReturnOrdersPostRequest;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.entity.response.OrderinfoReturnOrderPagesBean;
import com.boc.langxiang.entity.response.ReturnOrdersPageContent;
import com.boc.langxiang.net.ApiService;
import com.boc.langxiang.net.HandleErrorSubscriber;
import com.boc.langxiang.net.NetClient;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 申请退款
 */
public class OrdersApplyRefundPresenter implements OrdersApplyRefundContract.Presenter {

    private ApiService mService;
    private OrdersApplyRefundContract.View mView;

    public OrdersApplyRefundPresenter(OrdersApplyRefundContract.View view) {
        mService = NetClient.getInstance().net().create(ApiService.class);
        this.mView = view;
    }

    @Override
    public void returnOrdersPage(String orderid) {
        mView.showLoading();
        ReturnOrdersPageRequest ordersPageRequest = new ReturnOrdersPageRequest();
        ordersPageRequest.orderid = orderid;
        Observable<BaseResponse<ReturnOrdersPageContent>> ordersPage = mService.returnOrdersPage(ordersPageRequest.params());
        ordersPage.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse<ReturnOrdersPageContent>>(mView) {
                    @Override
                    public void onSuccess(BaseResponse<ReturnOrdersPageContent> response) {
                        ReturnOrdersPageContent content = response.getContent();
                        OrderinfoReturnOrderPagesBean orderinfo = content.getOrderinfo();
                        mView.getReturnOrdersInfo(orderinfo);
                    }
                });
    }

    @Override
    public void returnOrdersPost(String orderid, String content, String photo, String remark, String price, String totalprice, String pay, String status) {

        mView.showLoading();
        ReturnOrdersPostRequest ordersPostRequest = new ReturnOrdersPostRequest();
        ordersPostRequest.orderid = orderid;
        ordersPostRequest.content = content;
        ordersPostRequest.photo = photo;
        ordersPostRequest.remark = remark;
        ordersPostRequest.price = price;
        ordersPostRequest.totalprice = totalprice;
        ordersPostRequest.pay = pay;
        ordersPostRequest.status = status;
        Observable<BaseResponse> ordersPage = mService.returnOrdersPost(ordersPostRequest.params());
        ordersPage.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HandleErrorSubscriber<BaseResponse>(mView) {
                    @Override
                    public void onSuccess(BaseResponse response) {
                        mView.returnOrdersPostSuccess();
                    }
                });

    }
}
